package com.cc.fajian.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cc.fajian.pojo.QuestionOptionsInfoPo;
import com.cc.fajian.pojo.User;
import com.cc.fajian.service.IQusetionService;
import com.cc.fajian.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private IQusetionService queService;

    @RequestMapping(value = "/content",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> content(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        Map<String,Object> map = new HashMap<String, Object>();
        if(StringUtils.isEmpty(id)||StringUtils.isEmpty(type)){
            map.put("success",false);
            map.put("msg","必输项为空");
            return map;
        }
        QuestionOptionsInfoPo po = new QuestionOptionsInfoPo();
        po.setQuestionId(id);
        po.setQuestionType(type);
        po = queService.getQueById(po);
        System.out.println(po.getOptionsType());
        String[] optionsContent = po.getOptionsType().split("\\|");
        String[] optionsValue =  po.getOptionsValue().split("\\|");
        if(!optionsContent[0].isEmpty()) {
            Map<String, Object> map2 = new HashMap<String, Object>();
            for (int i = 0; i < optionsContent.length; i++) {
                map2.put(optionsContent[i], optionsValue[i]);
            }
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            list.add(map2);
            map.put("options", list);
        }
        map.put("text",po.getQuestionText());
        map.put("success", true);
        return map;
    }

}
